from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine
from os.path import abspath, dirname
from wiki import app

db = SQLAlchemy(app)

class UserDB(db.Model):
	__tablename__ = 'user'
	Id = db.Column('Id', db.Integer, primary_key=True, autoincrement=True)
	username = db.Column('username', db.Text)
	email = db.Column('email', db.Text)
	password = db.Column('password', db.Text)
	fullname = db.Column('fullname', db.Text)
	description = db.Column('description', db.Text)
	addresses = db.relationship('WikiPostDB', backref='owner', lazy='dynamic')
	privilege = db.Column('privilege', db.Text)
	
	def __init__(self, username, email, password, fullname, description, privilege):
		self.username = username
		self.email = email
		self.password = password
		self.fullname = fullname
		self.description = description
		self.privilege = privilege

class QueueWikiPostDB(db.Model):
	__tablename__ = 'queue_wiki_post'
	Id = db.Column('Id', db.Integer, primary_key=True, autoincrement=True)
	content = db.Column('content', db.Text)
	url = db.Column('url', db.Text)
	address = db.Column('address', db.Text)
	change_history = db.Column('change_history', db.Text)
	creator_id = db.Column(db.INT, db.ForeignKey('user.Id'))	
	default_lang = db.Column('default_lang', db.Text)
	orginal_id = db.Column('orginal_id', db.Text)

	def __init__(self, content, url, address, change_history, creator_id, default_lang, orginal_id):
		self.content = content
		self.url = url
		self.address = address
		self.change_history = change_history
		self.creator_id = creator_id	
		self.default_lang = default_lang
		self.orginal_id = orginal_id

class WikiPostDB(db.Model):
	__tablename__ = 'wiki_post'
	Id = db.Column('Id', db.Integer, primary_key=True, autoincrement=True)
	content = db.Column('content', db.Text)
	url = db.Column('url', db.Text)
	address = db.Column('address', db.Text)
	change_history = db.Column('change_history', db.Text)
	creator_id = db.Column(db.INT, db.ForeignKey('user.Id'))	
	default_lang = db.Column('default_lang', db.Text)

	def __init__(self, content, url, address, change_history, creator_id, default_lang):
		self.content = content
		self.url = url
		self.address = address
		self.change_history = change_history
		self.creator_id = creator_id	
		self.default_lang = default_lang

@app.before_request
def create_db():
	engine = create_engine('sqlite:///'+str(dirname(abspath(__file__)))+'/'+app.config['SQLALCHEMY_DATABASE_URI'].split('/')[-1])
	if not engine.dialect.has_table(engine, 'user') or not engine.dialect.has_table(engine, 'wiki_post') or \
	 not engine.dialect.has_table(engine, 'queue_wiki_post'):
		db.create_all()
	query = UserDB.query.filter_by(email=app.config['ROOT_USER']['email'], password=app.config['ROOT_USER']['password'], 
								   username=app.config['ROOT_USER']['username']).all()
	if len(query) <= 0:
		db.session.add(UserDB(app.config['ROOT_USER']['username'], app.config['ROOT_USER']['email'], 
							  app.config['ROOT_USER']['password'], app.config['ROOT_USER']['fullname'], 
							  app.config['ROOT_USER']['description'], app.config['ROOT_USER']['privilege']))
		db.session.commit()